#pragma once

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>

#include <mutex>
 
#define maxStackBufferSize       4*1024

namespace logcplus
{
	class logcplus_buffer
	{
	private:
		char* m_buffer;
		char m_stack_buffer[maxStackBufferSize];
		uint64_t m_cur_buffer_capacity;
		uint64_t m_cur_buffer_data_len;
		std::mutex m_lock;
	public:
		logcplus_buffer();
		logcplus_buffer(uint64_t nBufferCapacity);
		logcplus_buffer(const logcplus_buffer& ilogcplus_buffer);
		~logcplus_buffer();

		logcplus_buffer& operator=(const logcplus_buffer& ilogcplus_buffer);
		inline char* get_buffer();
		inline uint64_t  get_buffer_data_len();
		inline void  set_buffer_capacity(uint64_t nBufferCapacity);
		int init_log_buffer();
		int free_log_buffer();
		int write_buffer(const char* data, FILE* fp=nullptr);
	};

	inline char* logcplus_buffer::get_buffer(){ return (m_cur_buffer_capacity > maxStackBufferSize) ? m_buffer: m_stack_buffer;};
	inline uint64_t  logcplus_buffer::get_buffer_data_len(){return m_cur_buffer_data_len;};
	inline void  logcplus_buffer::set_buffer_capacity(uint64_t nBufferCapacity){m_cur_buffer_capacity = nBufferCapacity;};
}

